// house_pkg/pages/locate/index.ts

import qqMapSdk from '../../../utils/qqmapSdk'
Page({
  data: {
    list:[],
    address:'',
  },
  async onLoad() {
    // 调用 API
    const {latitude, longitude} = await wx.getLocation({type:'gcj02',isHighAccuracy:true})    
    this.search({latitude,longitude})
    this.getPoint({latitude,longitude})
  },
  // 重新定位
  async chooseAddress() {
    const { name,latitude,longitude}  = await wx.chooseLocation();
    this.setData({
      address:name
    })
    this.search({latitude,longitude})   
  },

  // 逆地理编码  通过经纬度获取文字信息
  getPoint({latitude,longitude}) {
    qqMapSdk.reverseGeocoder({
      location: {latitude,longitude},
      success: (res) => {
        this.setData({
          address:res.result.address
        })
      },
    })
  },
  // 搜索附近位置信息
  search({latitude,longitude}) {
    qqMapSdk.search({
      location:{latitude,longitude},
      keyword:"住宅小区",
      page_size:5,
      success: (res) => {
        console.log(res);
        
        this.setData({
          list:res.data
        })  
      }
  })
  }
})